<template>
	<el-container>
		<el-container>
			<el-header>
				<div class="right-panel">
					<div class="right-panel-search">
						<el-input v-model="search.end_price" placeholder="搜索工序任务单号/物料名称" clearable></el-input>
						<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
						<el-button type="info" @click="refresh">重置</el-button>
						<el-radio-group v-model="isCollapse">
							<el-radio-button :label="0">所有文件</el-radio-button>
							<el-radio-button :label="1">未开始任务</el-radio-button>
							<el-radio-button :label="2">执行中任务</el-radio-button>
							<el-radio-button :label="3">已完成任务</el-radio-button>
						</el-radio-group>
					</div>
				</div>
				<div class="left-panel">
					<el-button type="primary" :disabled="selection.length == 0" @click="start" v-auth="'processPlan.start'">开始</el-button>
					<!-- <el-button type="primary" :disabled="selection.length == 0" @click="status(2)" v-auth="'processPlan.end'">结束</el-button> -->
					<!-- <el-button type="primary" @click="add" v-auth="'spare.add'">新增</el-button> -->
					<el-button type="primary" @click="batch_show" :disabled="selection.length !== 1" v-auth="'processPlan.show'">查看</el-button>
					<el-button type="primary" :disabled="selection.length !== 1" @click="batch_edit" v-auth="'processPlan.edit'">修改</el-button>
					<el-button type="danger" :disabled="selection.length == 0" @click="batch_del" v-auth="'processPlan.delete'">删除</el-button>
				</div>
			</el-header>
			<el-main class="nopadding">
				<scTable @row-dblclick="dblclick" ref="table" :apiObj="apiObj" row-key="id"  @selection-change="selectionChange" stripe remoteSort remoteFilter border>
					<el-table-column fixed="left" type="selection" width="50" reserve-selection align="center"></el-table-column>
					<el-table-column label="操作" fixed="left" align="center" width="170">
						<template #default="scope">
							<el-button-group>
								<el-button text type="primary" size="small" @click="table_work(scope.row, scope.$index)">报工</el-button>
								<el-button text type="warning" size="small" @click="table_turn(scope.row, scope.$index)">转出</el-button>
								<el-button text type="primary" size="small" @click="table_house(scope.row, scope.$index)" v-if="scope.row.route_detail.is_last == 1">入库</el-button>
							</el-button-group>
						</template>
					</el-table-column>
					<el-table-column label="工序任务单号" prop="machin.production_code" width="110" align="center"></el-table-column>
					<el-table-column label="工序计划单号" prop="work_task_code" width="125" align="center"></el-table-column>
					<el-table-column label="工序名称" prop="work.work_name" align="center" width="100"></el-table-column>
					<el-table-column label="工序状态" prop="work.status" align="center">
                        <template #default="scope">
                            <el-tag :type="scope.row.work.status == 0 ? 'success' : 'danger'">{{scope.row.work.status == 0 ?'启用':'禁用'}}</el-tag>
                        </template>
                    </el-table-column>
					<!-- <el-table-column label="已报数量" prop="work_num" align="center" width="100"></el-table-column>
					<el-table-column label="合格数量" prop="qualified_num" align="center" width="100"></el-table-column>
					<el-table-column label="不合格数量" prop="rejects_num" align="center" width="100">
                        <template #default="scope">
                            {{scope.row.work_num - scope.row.qualified_num}}
                        </template>
                    </el-table-column> -->
					<el-table-column label="是否委外" prop="work.is_out" align="center">
                        <template #default="scope">
                            <el-tag :type="scope.row.work.is_out == 1 ? 'success' : 'danger'">{{scope.row.work.is_out == 1 ?'是':'否'}}</el-tag>
                        </template>
                    </el-table-column>
					<el-table-column label="报工权限" prop="dept" align="center" width="120">
                        <template #default="scope">
                            <!-- {{scope.row.work.dept}} {{scope.row.work.admin}} -->
                            {{scope.row.work.work_power == 1 ? '班组' : '人员'}}
                        </template>
                    </el-table-column>
					<el-table-column label="工艺文件" prop="work.enclosure" align="center" width="100">
                        <template #default="scope">
                            <el-button text size="small" type="primary" @click="showFile(scope.row.work.enclosure)">查看附件</el-button>
                        </template>
                    </el-table-column>
					<el-table-column label="工艺路线" prop="route.route_name" align="center" width="100"></el-table-column>
					<el-table-column label="物料编码" prop="product.goods_code" align="center" width="100"></el-table-column>
					<el-table-column label="物料名称" prop="product.goods_name" align="center" width="100"></el-table-column>
					<el-table-column label="物料类别" prop="product.cate_name" align="center"></el-table-column>
					<el-table-column label="规格型号" prop="product.goods_spec" align="center"></el-table-column>
					<el-table-column label="计划开工日期" prop="machin.plan_start" align="center" width="120"></el-table-column>
					<el-table-column label="计划完工日期" prop="machin.plan_end" align="center" width="120"></el-table-column>
					<el-table-column label="单位" prop="product.unit_name" align="center"></el-table-column>
					<el-table-column label="数量" prop="machin.number" align="center">
                        <template #default="scope">
                            {{ scope.row.machin&&scope.row.machin.number * Number(scope.row.route_detail&&scope.row.route_detail.match) }}
                        </template>
                    </el-table-column>
					<!-- <el-table-column label="顺序号" prop="customAddress" align="center"></el-table-column> -->
					<el-table-column label="产出比例" prop="route_detail.match" align="center"></el-table-column>
					<!-- <el-table-column label="前置工序顺序号" prop="customAddress" align="center" width="120"></el-table-column> -->
					<el-table-column label="加工说明" prop="route_detail.maching" align="center"></el-table-column>
					<!-- <el-table-column label="车间名称" prop="customAddress" align="center"></el-table-column> -->
					<el-table-column label="操作工" prop="staff" align="center"></el-table-column>
					<el-table-column label="时间单位" prop="route_detail.time_unit" align="center"></el-table-column>
					<el-table-column label="标准工时" prop="route_detail.standard_time" align="center"></el-table-column>
					<!-- <el-table-column label="标准总工时" prop="customAddress" align="center" width="100"></el-table-column> -->
					<!-- <el-table-column label="工序计划数量" prop="customAddress" align="center" width="120"></el-table-column> -->
					<!-- <el-table-column label="良品数" prop="customAddress" align="center">
                        <template #default="scope">
                            {{ (scope.row.machin&&scope.row.machin.number * Number(scope.row.route_detail&&scope.row.route_detail.match)) - scope.row.rejects_num }}
                        </template>
                    </el-table-column>
					<el-table-column label="不良品数" prop="rejects_num" align="center"></el-table-column> -->
					<!-- <el-table-column label="返修数" prop="customAddress" align="center"></el-table-column> -->
					<el-table-column label="是否首道工序" prop="customAddress" align="center" width="120">
                        <template #default="scope">
                            <el-tag :type="scope.row.route_detail&&scope.row.route_detail.is_first == 1 ? 'success' : 'danger'">{{scope.row.route_detail&&scope.row.route_detail.is_first == 1 ?'是':'否'}}</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column label="是否末道工序" prop="customAddress" align="center" width="120">
                        <template #default="scope">
                            <el-tag :type="scope.row.route_detail&&scope.row.route_detail.is_last == 1 ? 'success' : 'danger'">{{scope.row.route_detail&&scope.row.route_detail.is_last == 1 ?'是':'否'}}</el-tag>
                        </template>
                    </el-table-column>
					<el-table-column label="是否检验" prop="customAddress" align="center">
                        <template #default="scope">
                            <el-tag :type="scope.row.route_detail&&scope.row.work.is_quality == 1 ? 'success' : 'danger'">{{scope.row.route_detail&&scope.row.work.is_quality == 1 ?'是':'否'}}</el-tag>
                        </template>
                    </el-table-column>
					<el-table-column label="变更次数" prop="customAddress" align="center"></el-table-column>
					<el-table-column label="单位计时工资" prop="work.labor_time" align="center" width="120"></el-table-column>
					<el-table-column label="单位计件工资" prop="work.labor_piece" align="center" width="120"></el-table-column>
					<!-- <el-table-column label="工序计划开工时间" prop="customAddress" align="center" width="130"></el-table-column>
					<el-table-column label="工序计划完工时间" prop="customAddress" align="center" width="130"></el-table-column> -->
					<el-table-column label="完工状态" prop="customAddress" align="center" width="120">
                        <template #default="scope">
                            <el-tag :type="scope.row.status == 1 ? 'warning' : scope.row.status == 2 ? 'success' : 'primary'">{{scope.row.status == 1 ?'未完工' : scope.row.status == 2 ? '生产中' : '生产中'}}</el-tag>
                        </template>
                    </el-table-column>
				</scTable>
			</el-main>
		</el-container>
	</el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false" draggable></save-dialog>
    <select-hum v-if="dialog.hum" ref="humDialog" @success="handleSuccess" @closed="dialog.hum = false" draggable></select-hum>
</template>

<script>
import SelectHum from './selectHum.vue';
import saveDialog from "./save.vue";
export default {
	name: 'user',
	components: {
		saveDialog,SelectHum
	},
	data() {
		return {
			dialog: {
				save: false,
				hum: false,
			},
			group: [],
			apiObj: this.$API.production.processplan.list,
			selection: [],
			search: {
				title: null,
				cid: null,
			},
			isCollapse:0
		}
	},
	methods: {
		/**报工*/
		table_work(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(row)
			})
		},
        /**转出*/
		table_turn(row) {
			this.dialog.hum = true
			this.$nextTick(() => {
				this.$refs.humDialog.open('add').setData(row)
			})
		},
        // 入库
        table_house(row) {
            let form = row
			form.zhuangtai = ''
			this.$router.push({
				path: '/process/project/inHouse',
				query: {
                    form:JSON.stringify(form)
                }
			});
		},
		/**查看点击*/
		batch_show() {
			this.$router.push({
				path: '/process/addProject',
			});
		},
		//添加
		add() {
			this.$router.push({
				path: '/process/addProject',
			});
		},
		//编辑
		table_edit() {
			this.$router.push({
				path: '/process/addProject',
			});
		},
		/**双击事件*/
		dblclick(row) {
			this.table_show(row)
		},
		//查看
		table_show(row) {
			let form = row
			form.zhuangtai = 'show'
			this.$router.push({
				path: '/bom/addBom',
				query: form
			});
		},
		//表格内开关
		async changeSwitch(val, row) {
			row.$switch_status = true;
			var res = await this.$API.basicdata.applyList.update_status.post({
				id: row.id,
				system_disable: row.system_disable,
			});
			if (res.code == 1) {
				row.$switch_status = false;
				this.$refs.table.refresh();
				this.$message.success("操作成功");
			} else {
				this.$alert(res.message, "提示", {type: "error"});
			}
		},
		//删除
		async table_del(row, index) {
			var reqData = {id: row.id}
			var res = await this.$API.basicdata.applyList.delete.post(reqData);
			if (res.code == 1) {
				//这里选择刷新整个表格 OR 插入/编辑现有表格数据
				this.$refs.table.tableData.splice(index, 1);
				this.$refs.table.refresh();
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
		//本地更新数据
		handleSuccess(data, mode) {
			if (mode == 'add') {
				this.$refs.table.refresh()
			} else if (mode == 'edit') {
				this.$refs.table.refresh()
			} else if (mode == 'set') {
				this.$refs.table.refresh()
			}
			this.printArr = ''
		},
		selectionChange(selection) {
			this.selection = selection;
		},
        showFile(row){
            console.log('row :>> ', row);
            if(row){
                window.open(row)
            }
        },
        async table_start_all(data) {
			var reqData = {work_plan_id: data.ids}
            var res = await this.$API.production.processtime.add.post(reqData);
			if (res.code == 1) {
				this.$refs.table.refresh()
				this.$message.success("操作成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		//批量开始
		async start() {
			var ids = [];
			this.selection.forEach((item) => {
				ids.push(item.id)
			})
			this.$confirm(`确定开始选中的 ${this.selection.length} 项吗？`, '提示', {
				type: 'warning'
			}).then(() => {
				this.table_start_all({ids:ids})
			}).catch(() => {

			})
		},
	}
}
</script>

<style>
.search-input {
	width: auto;
	font-size: 14px;
}

.shopInfo {
	display: flex;
	align-items: center;
}

.img {
	width: 100px;
	height: 100px;
	margin-right: 5px;
}
</style>
